package com.dzzw.Dao;

import com.dzzw.Model.Forum;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//论坛主题
public class ForumDao {
    private final Connection con = DButils.getCon();
    //新增论坛主题
    public void ForumAdd(Forum forum){
        Date forum_sendtime = new Date();
        SimpleDateFormat SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String sql = "insert into t_forum(user_id,forum_title,forum_content,forum_sendtime) values(?,?,?,?)";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,forum.getUser_id());
            preparedStatement.setString(2,forum.getForum_title());
            preparedStatement.setString(3,forum.getForum_content());
            preparedStatement.setDate(4,java.sql.Date.valueOf(SimpleDateFormat.format(forum_sendtime.getTime())));
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            System.out.println("数据添加失败！");

            e.printStackTrace();
        }
    }

    //修改已发布主题信息
    public void ForumModify(Forum forum){
        String sql = "update t_forum set forum_title=?,forum_content=? where forum_id=?";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1,forum.getForum_title());
            preparedStatement.setString(2,forum.getForum_content());
            preparedStatement.setInt(3,forum.getForum_id());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //删除论坛主题
    public void ForumDelete(int forum_id){
        String sql = "Delete from t_forum where forum_id = ?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,forum_id);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //根据用户id查询论坛主题信息
    public static List<Forum> SelectAllForumById(int user_id){
        List<Forum> list = new ArrayList<>();
        String sql = "SELECT f.forum_id,u.user_name,f.forum_title,f.forum_content,f.forum_sendtime FROM t_forum AS f INNER JOIN t_user AS u ON f.user_id = u.user_id where f.user_id = ?";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,user_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Forum forum = new Forum();
                forum.setForum_id(resultSet.getInt(1));
                forum.setUser_name(resultSet.getString(2));
                forum.setForum_title(resultSet.getString(3));
                forum.setForum_content(resultSet.getString(4));
                forum.setForum_sendtime(resultSet.getDate(5));
                list.add(forum);
            }
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //查询全部论坛主题
    public static List<Forum> SelectAllForum(){
        List<Forum> list = new ArrayList<>();
        String sql = "SELECT f.forum_id,u.user_name,f.forum_title,f.forum_content,f.forum_sendtime FROM t_forum AS f INNER JOIN t_user AS u ON f.user_id = u.user_id";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Forum forum = new Forum();
                forum.setForum_id(resultSet.getInt(1));
                forum.setUser_name(resultSet.getString(2));
                forum.setForum_title(resultSet.getString(3));
                forum.setForum_content(resultSet.getString(4));
                forum.setForum_sendtime(resultSet.getDate(5));
                list.add(forum);
            }
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
